#
#  TunesController.rb
#  TunesServer
#
#  Created by Tom Demeyer on 3/28/08.
#  Copyright (c) 2008 __MyCompanyName__. All rights reserved.
#

require 'ITunesServer'
require 'net/http'

class TunesController  < OSX::NSObject

  ib_outlets :window, :port, :symbServer, :numServer
  ib_outlets :serverState, :toggleButton
  ib_outlets :statslabel, :access, :portView

  def awakeFromNib
  
	Dir.chdir( OSX::NSBundle.mainBundle.resourcePath.fileSystemRepresentation )
	setUrls
	showServerState
	
	OSX::NSApplication::sharedApplication.activateIgnoringOtherApps(true)
	@window.orderFrontRegardless
	@window.makeKeyWindow
	
	
  end
  
  
  def setUrls

	if( File.file?(PORT_FILE) )
		p = IO.read(PORT_FILE)
		@port.setStringValue p
	end


	hostname = Socket.gethostname
	host = "http://" + hostname + ":" +  @port.stringValue + "/"
	@symbServer.setStringValue host

	host = "http://" + IPSocket.getaddress(hostname) + ":" + @port.stringValue + "/"
	@numServer.setStringValue host
	
	
  end

  def aboutApp(sender)
    NSApp.orderFrontStandardAboutPanelWithOptions(nil)
    #NSApp.orderFrontStandardAboutPanelWithOptions("ApplicationVersion" => "Version #{VERSION}")
  end
  ib_action :aboutApp
  
  
  

  def showServerState
	if( running? )
		@serverState.setTextColor NSColor.greenColor
		@serverState.setStringValue "Running"
		@toggleButton.setTitle "Stop Server"
		@port.setEnabled(false)
		@portView.setHidden(true)
		@access.setHidden(false)
		while(true)
			begin
				setStats
				break
			rescue Exception => exp
				puts exp.message
				sleep(1)
			end
		end
	else
		@serverState.setTextColor NSColor.redColor
		@serverState.setStringValue "Stopped"
		@toggleButton.setTitle "Start Server"
		@port.setEnabled(true)
		@statslabel.setHidden(true)
		@access.setHidden(true)
		@portView.setHidden(false)
	end
  end

  def running?
    if File.file?(PID_FILE)
		return true
	end
	return false
  end

  def openLink
	system "open #{@symbServer.stringValue}"
  end
  ib_action :openLink
  
  
  def setStats
	url = URI.parse( "http://127.0.0.1:#{@port.stringValue}/tunes/control?op=stats" )
	s = Net::HTTP.get(url)
	@statslabel.setStringValue(s)
	@statslabel.setHidden(false)
  end


  def getStartupState
	Thread.start do 
		@serverState.setTextColor NSColor.blackColor
		@serverState.setStringValue "scanning..."
		
		while(true)
			begin
				setStats
				break
			rescue Exception => exp
				sleep(1)
			end
		end
		
		@toggleButton.setEnabled(true)
		showServerState
	end
  end

  def toggleServer(sender)

	if( running? )
		p = IO.read(PID_FILE).to_i rescue nil
		p && Process.kill("TERM", p)
		i = 0
		while File.file?(PID_FILE)
			sleep(1)
			i++
			FileUtils.rm(PID_FILE) if( i == 3 )
		end
		showServerState
	else 
		port = @port.stringValue
		if( port.to_i < 1024 ) 
			port = "2000"
			@port.setStringValue "2000"
		end
		@toggleButton.setEnabled(false)
		@portView.setHidden(true)
		File.open(PORT_FILE, 'w') {|f| f << port}
		system "ruby ./iTunesServer.rb #{port} &"
		while !File.file?(PID_FILE)
			sleep(1)
		end
		setUrls
		getStartupState
	end
	
	OSX::NSApplication::sharedApplication.activateIgnoringOtherApps(true)
  end
  ib_action :toggleServer

end


